var base=getApp();
var curpage = 1;//当前页数
var size = 10;//每页显示条数
var gid =0;
var list=[];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    commentList:[],
    url: base.path.url,
    isReach: 1,//是否允许下拉 0为不允许
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this;
    gid = e.id;
    curpage = 1;
    that.getList();
  },
  onReachBottom: function () {
    //下拉
    var that = this;
    var isReach = that.data.isReach;
    if (isReach == 0) {
      return;
    }
    that.getList()
  },
  onShareAppMessage: function () {
    return {
      title: base.user.shareTitle,
      desc: base.user.shareDesc,
      path: '/pages/index/index',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  getList: function () {
    var that=this;
    var list = that.data.commentList;
    wx.request({
      url: base.path.api + 'Goods.GetComments',
      data: {
        gid: gid,
        size: size,
        curpage: curpage
      },
      async: false,
      method: "POST",
      header: base.user.header,
      success: function (res) {
        var result = res.data;
        var comment = result.data;
        var commentList = [];
        if (result.ret == 200) {
          var result = res.data;
          var commentList = [];
          if (result.data.length >= 0) {
            for (var i = 0; i < result.data.length; i++) {
              var nickname = "";
              var headimg = "";
              if (result.data[i].is_anonymous == 1) {
                nickname = "匿名用户";
              } else {
                nickname = result.data[i].nickname;
              }
              if (result.data[i].headimg.indexOf("http") >= 0) {
                headimg = result.data[i].headimg;
              } else {
                headimg = that.data.url + result.data[i].headimg;
              }
              var create_time = base.getDateList(parseInt(result.data[i].create_time), parseInt(result.data[i].now));
              commentList.push({ "content": result.data[i].content, "headimg": headimg, "star": result.data[i].star, "create_time": create_time, "nickname": nickname });
            }
            list = list.concat(commentList);
            that.setData({
              commentList: list,
            });
          }
          if (result.data.length >= size) {
            curpage++;
            that.data.isReach = 1;
          } else {
            that.data.isReach = 0;
          }
        } else {
          wx.showToast({
            title: result.msg,
            icon: 'loading',
            duration: '1000',
            success: function () {
              wx.navigateBack({
                url: '../index/index'
              });
            }
          });
        }
      }
    })
  }
});

